Skip to content

feat(backend): support asset custom fields in request forms - #548

Merged
stefan-ernst merged 6 commits into
mainfrom
feat/portal-asset-fields
Nov 20, 2026
Merged

stefan-ernst merged 6 commits into
mainfrom
feat/portal-asset-fields

Conversation

@stefan-ernst

@stefan-ernst stefan-ernst commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What

Portal request types can now bind asset-type custom fields, and the customer portal renders a working asset picker for them. The picker honors the field's ql_query, scoped to the field's asset set.

Why

The customer portal could only offer title/description plus a subset of custom fields. Asset fields were excluded from the portal-fillable allowlist and had no portal-backed option source, so a bug-report form could not ask the customer to pick the application/asset.

How asset fields are exposed

The per-set "Available on portals" grant (asset_set_portal_access) is the exposure switch. Previously it only gated asset reports ("a grant alone never exposes a set"). It now also authorizes asset custom fields:

  • A field bound to a granted set resolves assets directly — no report needed.
  • The field's admin-authored ql_query is evaluated server-side with the portal context and confined to the field's set.
  • The picker passes field_id, not a raw query or set id, so a caller cannot inject CQL or point at another set.
  • The field must be exposed on the portal (bound to a visible request type/asset report whose effective create screen includes it).
  • Submission validation authorizes the same way: the set must carry a portal grant and the asset must belong to the field's set.

The asset-set UI description is updated to say the grant covers reports and asset fields.

Changes

Backend

  • IsPortalFormFillableCustomFieldType + PublicFormFillableCustomFieldType(allowAssets); AllowedCreateScreenCustomFieldIdentifiers and availableCreateFields take allowAssets. Portal channels allow asset; public form channels do not.
  • Generalized portalReportCQLSQL into portalAssetCQLSQL(ctx, setID, query, ...) so a field's ql_query is evaluated with the same portal context as a report.
  • GET /portal/{slug}/assets/search?field_id=… loads the field's set and ql_query, checks the set grant, evaluates the query, and returns matching assets. Without field_id it keeps the report-scoped text search.
  • Portal submission validates asset custom-field values against the set grant and set membership.

Fallback screen

  • availableCreateFields, AllowedCreateScreenCustomFieldIdentifiers, and collectBoundCustomFieldIDs now use GetEffectiveCreateScreenID, so fields bound to the configuration set's default create screen resolve when the item type has no override. Previously only a per-item-type override worked, which is why custom fields could vanish from the builder/form.

Frontend

  • portal.searchAssets(slug, { fieldId, query, assetSetId, limit }).
  • CustomFieldRenderer passes the field id to loadAssetOptions; FormFields forwards it, and the portal request form and portal asset-report form supply a loader backed by the portal asset search. Public forms leave it unset.

Tests

  • Asset field picker resolves from the set grant and applies the stored ql_query; revoking the grant hides options.
  • Submission validation denies assets outside the field's set and sets without a grant.
  • Portal vs. form allowlist and submission separation.
  • Asset id extraction (scalar/object/array).
  • Portal custom-field exposure includes asset fields.
  • Create-screen fallback resolution for inherited default and explicit item-type override.
  • portal.searchAssets URL contract.

Verified with go build/go vet/gofmt, svelte-check, the internal/services + internal/handlers Go suites, focused black-box request-type/screen tests, and the forms/portal Vitest suites.

Companion tests: Windshiftapp/core-tests-internal#66.

Review follow-ups

  • The report's asset-set grant is checked inside the report listing query, so custom-field loading needs no second read connection while the cursor is open (--max-read-conns=1).
  • The picker and submission share the asset CQL identity, so a linked customer on an internal session resolves the same currentUser()/currentCustomer()/currentOrganisation() on both paths. Item attribution still uses the request auth context.
  • Form-mode asset reports validate asset parameters against the field's set grant and query before substituting them into the report CQL.
  • getPortalCustomerID is nil-safe for handlers built without a session manager.

Second review follow-ups

  • Form-mode asset reports validate only the custom fields bound to the report schema, so a virtual field with a numeric identifier can't be mistaken for an asset reference.
  • Multi-asset fields are excluded from asset report forms: their list values are not expressible in report CQL substitution. Request types keep multi-asset fields.
  • Screen fields carry the joined custom-field options so arity is available without a second lookup.

Portal request types can now bind asset-type custom fields, and the
customer portal renders an asset picker backed by the portal's own asset
search, scoped to the field's configured set and the visitor's visible
reports. Submissions validate that every referenced asset belongs to the
field's set and is visible to the submitting visitor.

Resolve the create screen through the configuration set's default when
the item type has no override, so fields bound to the default create
screen no longer disappear from the form builder and public forms.
@github-actions

Copy link
Copy Markdown

Size Change: 0 B 🆕

Total Size: 0 B

compressed-size-action

Asset custom fields previously resolved picker options only through a
visible asset report and ignored the field's ql_query. Instead, treat the
existing per-set "available on portals" grant as the exposure switch:
a field bound to a granted set resolves assets directly, evaluating the
field's admin-authored ql_query server-side and scoping it to the set.

The picker now passes field_id rather than a set id, so the server owns
the query and callers cannot inject CQL. Submission validation authorizes
against the same grant plus set membership instead of report visibility.

Update the asset-set portal-access description to cover asset fields.

@stefan-ernst stefan-ernst left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found two cases where the picker can expose assets beyond the configured form or filter, and two gaps in request submission validation. The inline comments identify the checks to share between the read and write paths. This was a source review; I did not run the test suites.

Comment thread internal/handlers/portal_assets.go Outdated
}
var options struct {
AssetSetID int `json:"asset_set_id"`
QLQuery string `json:"ql_query"`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 — Honor the legacy cql_query option. The field editor and shared renderer both recognize cql_query, but this endpoint only reads ql_query. For an existing field configured with only cql_query, the empty query matches every asset in the granted set instead of applying the configured restriction. Use the same option resolution as the picker and add coverage for a field with only the legacy key.

}
for _, route := range visibleARRoutes {
if err := s.collectBoundCustomFieldIDs(ctx, cfIDs, "asset_report", route.id, route.workspaceID, route.itemTypeID); err != nil {
if err := s.collectBoundCustomFieldIDs(ctx, cfIDs, "asset_report", route.id, route.workspaceID, route.itemTypeID, true); err != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 — Require the owning report to be accessible before it can expose picker options. GetCustomFieldsForChannel includes bindings from an active report even if the report's own asset set no longer has a portal grant. The picker checks only the field's set; when those sets differ, revoking the report's grant hides the form but leaves the options readable. Use the same grant and binding checks as resolvePortalAssetReport before treating the report as an exposure source, and cover revocation with distinct report and field sets.

Comment thread internal/handlers/portal_assets.go Outdated
if err != nil {
return fmt.Errorf("resolve asset field %s: %w", key, err)
}
if setID != fieldSetID {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Enforce the picker filter when saving a request. This accepts any asset in the granted set, including rows excluded by the field's configured query. A field scoped by currentOrganisation() can therefore store a reference to another organisation's asset in the same set. Reuse the field's CQL predicate with the submitter's portal context and add a denial test for a same-set asset that the picker excludes.

Comment thread internal/handlers/portal_assets.go Outdated
AssetSetID int `json:"asset_set_id"`
}
if err := json.Unmarshal([]byte(options), &opts); err != nil || opts.AssetSetID <= 0 {
continue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Reject asset fields that cannot be validated. An invalid or missing asset_set_id skips the grant and membership checks entirely. Separately, portalAssetFieldValueIDs silently drops malformed entries, so a nonblank value with no valid asset ID can satisfy a required field and be persisted unchanged. Fail closed on invalid asset configuration or value shape, and validate every submitted entry before storing it.

- Honor the legacy cql_query option so a field configured with only that
  key still applies its filter instead of matching every asset in the set.
- Require the owning report's asset set to keep its portal grant before
  the report can expose custom fields as picker options.
- Enforce the field's configured query on submission, so a same-set asset
  the picker excludes cannot be stored.
- Fail closed when an asset field has no usable set or a submitted value
  contains no valid asset id.
Prefer the legacy cql_query over ql_query, the same order the shared
picker uses, so an unedited legacy field resolves identically through the
portal.

@stefan-ernst stefan-ernst left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed the follow-up commits. The legacy alias and malformed-input fixes look good, and the new checks cover the original cases in the companion tests. Three issues remain in the surrounding paths: a nested database read can stall under pool pressure, picker and submission use different identities for linked internal users, and form-mode reports still don't validate asset parameters against the picker policy. This was a source review; I did not run the tests.

Comment thread internal/services/portal_service.go Outdated
// The report's own set must still carry a portal grant. Without this
// check a revoked grant hides the report but leaves the fields it
// references exposed as picker options.
enabled, err := assetRepo.IsSetPortalEnabled(assetSetID)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 — Avoid taking a second read connection while arRows is open. IsSetPortalEnabled makes a non-contextual QueryRow against the same bounded pool that is still holding the report cursor. With --max-read-conns=1, even one visible form report stalls custom-field loading until the outer query context times out; the same can happen when concurrent requests occupy the pool. Put an EXISTS check for asset_set_portal_access in the report query so the grant check needs no extra connection.

Comment thread internal/handlers/portal.go Outdated
if portalCustomerID != nil {
customerOrgID = h.getPortalCustomerOrgID(ctx, *portalCustomerID)
}
if err := h.validatePortalAssetCustomFields(ctx, portalCustomerID, customerOrgID, validationResult.CustomFieldValues); err != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Resolve the same CQL identity for the picker and submission. The picker uses getPortalCustomerID, which can resolve a linked portal customer from an internal session. Here portalCustomerID comes from getAuthFromContext, which returns nil for an internal session even when the customer is linked. As a result, currentUser(), currentCustomer(), and currentOrganisation() can match in the picker but resolve to no identity on submit. Use a shared resolver for the filter context without changing request attribution, and cover the linked internal-user flow.

Comment thread internal/handlers/portal_assets.go Outdated
allowedCustomFieldIDs := make(map[string]struct{})
if report.ItemTypeID != nil && report.WorkspaceID != nil {
allowedCustomFieldIDs, err = services.AllowedCreateScreenCustomFieldIdentifiers(h.db, *report.WorkspaceID, *report.ItemTypeID)
allowedCustomFieldIDs, err = services.AllowedCreateScreenCustomFieldIdentifiers(h.db, *report.WorkspaceID, *report.ItemTypeID, true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Validate asset picker parameters before executing form-mode reports. This now admits asset fields into the report schema, but ExecuteAssetReport still checks only permitted parameter names and whether required values are blank before substituting them into CQL. A report that uses the selected asset ID to scope results does not enforce the field's set grant or configured query, so it can accept an asset the picker excludes. Apply the same asset-reference policy to report parameters before substitution, or keep asset fields out of report forms until that check is in place.

- Fold the report's asset-set grant into the report listing query so the
  grant check needs no second read connection while the cursor is open.
- Share the asset CQL identity between the picker and submission, so a
  linked customer on an internal session resolves the same
  currentUser()/currentCustomer()/currentOrganisation() on both paths.
- Validate asset parameters on form-mode asset reports against the same
  set-grant and field-query policy as the picker.
- Guard getPortalCustomerID against a nil session manager.

@stefan-ernst stefan-ernst left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connection-pool and picker/submission identity fixes look good, and the focused handler/service tests passed, including the new scalar report validation case. Two report parameter cases still need coverage and fixes: multi-asset picker values and numeric identifiers on virtual fields.

Comment thread internal/handlers/portal_assets.go Outdated
// policy before substituting it into the report query.
submitted := make(map[string]any, len(formValues))
for identifier, value := range formValues {
submitted[identifier] = value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Preserve multi-asset selections through validation. AssetReportFormModal sends an array of IDs for a multi-asset field even when only one asset is selected. readFormParams has already turned that array into JSON text by this point, so the asset validator treats it as one malformed ID and returns 400 for every nonempty selection. Keep the typed values through validation and define how lists are substituted into CQL, or exclude multi-asset fields from report forms until that is supported. Add a report test using the array shape the UI submits.

for identifier, value := range formValues {
submitted[identifier] = value
}
if err := h.validatePortalAssetCustomFields(ctx, portalCustomerID, customerOrgID, submitted); err != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Validate only parameters bound to custom asset fields. The report schema allows numeric identifiers on virtual fields, but this passes every parameter to a validator that looks up numeric keys as global custom-field IDs. An existing virtual text or select field can start returning 400 if its identifier matches an unrelated asset definition, even if that asset field isn't on the report's screen. Build the validation input from the loaded schema's custom rows, as request submission already does.

- Validate only custom fields bound to the report, so a virtual field
  whose identifier matches an unrelated asset custom-field id is never
  looked up as an asset reference.
- Exclude multi-asset fields from asset report forms: their list values
  are not expressible in report CQL substitution. Request types keep them.
- Join custom field options onto screen fields so arity is available
  without a second lookup.

@stefan-ernst stefan-ernst left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both regressions from the previous review are addressed for newly configured reports: numeric virtual fields skip asset validation, and the report builder excludes multi-asset fields. The remaining issues are for existing bindings whose field is changed from single to multi.

Focused handler, service, and black-box Go tests passed on this revision:

./overlay.sh ../core-asset-portal -- -tags=test -count=1 -run 'Test(PortalAssetFilterIdentityResolvesLinkedInternalCustomer|ExecuteAssetReportValidatesAssetFieldParams|ExecuteAssetReportIgnoresVirtualNumericIdentifiers|GetCustomFieldsForChannelRequiresReportSetGrant|SearchPortalAssetsByFieldEvaluatesStoredQuery|ValidatePortalAssetCustomFieldsDeniesUnavailableAssets|PortalAssetFieldValueIDs|AllowedCreateScreenCustomFieldIdentifiersExcludesMultiAssetForReports|RequestTypeAvailableFieldsCreateScreenResolution)$' ./internal/handlers ./internal/services ./tests

I did not rerun the full suites on this revision.

allowedCustomFieldIDs := make(map[string]struct{})
if report.ItemTypeID != nil && report.WorkspaceID != nil {
allowedCustomFieldIDs, err = services.AllowedCreateScreenCustomFieldIdentifiers(h.db, *report.WorkspaceID, *report.ItemTypeID)
allowedCustomFieldIDs, err = services.AllowedCreateScreenCustomFieldIdentifiers(h.db, *report.WorkspaceID, *report.ItemTypeID, true, true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Exclude unsupported report bindings from picker authorization too. The report schema now drops multi-asset fields, but the picker authorizes field_id through GetCustomFieldsForChannel. Its collectBoundCustomFieldIDs still counts persisted asset_report_fields rows without checking arity. If a field is switched from single to multi after being bound to a report, the form drops it but the picker can still return its assets even with no eligible form binding. Apply the same eligibility check when collecting report-owned fields, while keeping multi-asset fields available through request-type bindings.

// bound to this report are candidates: a virtual field with a numeric
// identifier must not be looked up as a global custom-field id.
submitted := make(map[string]any, len(fields))
for _, f := range fields {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Don't execute a report after silently dropping a required asset filter. If a bound single-asset field is changed to multi, the filtered schema no longer contains that required row. Execution then skips its required check and substitutes an empty string for its CQL placeholder; depending on the operator, that can broaden the results. Reject execution when a referenced required binding is no longer supported, or prevent the arity change while that binding exists.

@stefan-ernst stefan-ernst changed the title feat(portal): support asset custom fields in request forms feat(backend): support asset custom fields in request forms Nov 20, 2026
@stefan-ernst
stefan-ernst merged commit b40f490 into main Nov 20, 2026
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant